package com.ruoyi.web.controller.Base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.AbilityInstitution;
import com.ruoyi.service.IAbilityInstitutionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 机构Controller
 * 
 * @author ruoyi
 * @date 2024-08-20
 */
@RestController
@RequestMapping("/web/institution")
public class AbilityInstitutionController extends BaseController
{
    @Autowired
    private IAbilityInstitutionService abilityInstitutionService;

    /**
     * 查询机构列表
     */

    @GetMapping("/list")
    public TableDataInfo list(AbilityInstitution abilityInstitution)
    {
        startPage();
        List<AbilityInstitution> list = abilityInstitutionService.selectAbilityInstitutionList(abilityInstitution);
        return getDataTable(list);
    }

    /**
     * 导出机构列表
     */

    @Log(title = "机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AbilityInstitution abilityInstitution)
    {
        List<AbilityInstitution> list = abilityInstitutionService.selectAbilityInstitutionList(abilityInstitution);
        ExcelUtil<AbilityInstitution> util = new ExcelUtil<AbilityInstitution>(AbilityInstitution.class);
        util.exportExcel(response, list, "机构数据");
    }

    /**
     * 获取机构详细信息
     */

    @GetMapping( "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(abilityInstitutionService.selectAbilityInstitutionByID(ID));
    }

    /**
     * 新增机构
     */

    @Log(title = "机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AbilityInstitution abilityInstitution)
    {
        try {
            abilityInstitutionService.insertAbilityInstitution(abilityInstitution);
        }catch (Exception e){
            return error("机构账号重复");
        }

        return toAjax(1);
    }

    /**
     * 修改机构
     */

    @Log(title = "机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AbilityInstitution abilityInstitution)
    {
        return toAjax(abilityInstitutionService.updateAbilityInstitution(abilityInstitution));
    }

    /**
     * 删除机构
     */

    @Log(title = "机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {

        try {
            abilityInstitutionService.deleteAbilityInstitutionByID(IDs[0]);
        }catch (Exception e){
            return error("机构下有用户，无法删除");
        }
        return toAjax(1);
    }
}
